package com.atguigu.jxc.controller;

import com.alibaba.fastjson.JSON;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.OverflowList;
import com.atguigu.jxc.entity.OverflowListGoods;
import com.atguigu.jxc.service.OverflowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author: lei
 * @Date 2021/11/27 15:22
 **/
@RestController
public class OverflowController {

    @Autowired
    private OverflowService overflowService;


    /**
     *新增报溢单
     * 请求URL：http://localhost:8080/overflowListGoods/save?overflowNumber=BY1605767033015（报溢单号）
     * 请求参数：OverflowList overflowList, String overflowListGoodsStr
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：ServiceVO
     * Response Example Value：
     * {
     *     "code": 100,
     *     "msg": "请求成功",
     *     "info": null
     * }
     */
    @PostMapping("overflowListGoods/save")
    public ServiceVO save(HttpServletRequest request){
        String overflowListGoodsStr = request.getParameter("overflowListGoodsStr");
        List<OverflowListGoods> overflowListGoods = JSON.parseArray(overflowListGoodsStr, OverflowListGoods.class);

        // userId一直没法获取啊
        String overflowNumber = request.getParameter("overflowNumber");
        String overflowDate = request.getParameter("overflowDate");
        String remarks = request.getParameter("remarks");
        OverflowList overflowList = new OverflowList();
        overflowList.setOverflowDate(overflowDate);
        overflowList.setOverflowNumber(overflowNumber);
        overflowList.setRemarks(remarks);
        overflowList.setUserId(1);

        overflowService.save(overflowListGoods,overflowList);

        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS,null);

    }

    /**
     * 报溢单查询
     * 请求URL：http://localhost:8080/overflowListGoods/list
     * 请求参数：String  sTime（开始时间）, String  eTime（结束时间）
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：Map<String,Object>
     */
    @PostMapping("overflowListGoods/list")
    public Map<String,Object>  listGoods(HttpServletRequest request){
        String sTime = request.getParameter("sTime");
        String eTime = request.getParameter("eTime");

        Map map  = overflowService.listGoodsList(sTime,eTime);
        return map;

    }

    /**
     * 报溢单商品信息
     * 请求URL：http://localhost:8080/overflowListGoods/goodsList
     * 请求参数：Integer overflowListId
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：Map<String,Object>
     */
    @PostMapping("overflowListGoods/goodsList")
    public Map<String,Object> goodsList(HttpServletRequest request){
        String overflowListId = request.getParameter("overflowListId");
        Map map = overflowService.goodsList(overflowListId);
        return map;
    }
}
